﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureRoofEditModeBehavior.h"
#include "DbObjectUtils.h"
#include "StructureEditModeTypes.h"
#include "IEditMode.h"
#include "IModelLine.h"
#include "IGenericElement.h"
#include "IStructureSlopeLine.h"
#include "IElementParentReporter.h"
#include "IDocument.h"
#include "StructureRoofProfileUsedModelLineUtils.h"
#include "NdbUpgrade.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

BEGIN_UPGRADE_CLASS(gcmp, StructureRoofEditModeBehavior, 2)
ADD_NON_OBJECT_PROPERTY(ElementId, EditingFamilyId)
ADD_NON_OBJECT_PROPERTY(ElementId, EditingInstanceTypeId)
ADD_NON_OBJECT_PROPERTY(ElementId, PresetElementId)
END_UPGRADE_CLASS

DBOBJECT_DATA_DEFINE(StructureRoofEditModeBehavior)
{
}

DEFINE_STRUCTURE_EDIT_MODE_BEHAVIOR_METHODS(StructureRoofEditModeBehavior);

StructureRoofEditModeBehavior::~StructureRoofEditModeBehavior()
{
}

bool StructureRoofEditModeBehavior::StartEditing(gcmp::IDocument* pDoc, bool isCreating)
{
    DBG_WARN_AND_RETURN_UNLESS(nullptr != pDoc, false, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IEditMode* pProfileEditMode = IEditMode::Get(pDoc, GetEditModeType());
    DBG_WARN_AND_RETURN_UNLESS(nullptr != pProfileEditMode, false, L"pProfileEditMode为空",L"GDMPLab",L"2024-03-30");
    pProfileEditMode->SetIsCreating(isCreating);
    return true;
}

bool StructureRoofEditModeBehavior::EndEditing(gcmp::IDocument* pDoc)
{
    DBG_WARN_AND_RETURN_UNLESS(nullptr != pDoc, false, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    this->DeletePresetElementId(pDoc);

    return true;
}

std::wstring StructureRoofEditModeBehavior::GetEditModeType() const
{
    return StructureEditModeTypes::StructureRoofProfileModeType;
}

bool StructureRoofEditModeBehavior::IsElementEditable(const IElement* pElement) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");
    
    // 轮廓线可以编辑
    if (StructureRoofProfileUsedModelLineUtils::IsModelLineUsedForRoofProfile(pElement))
    {
        return true;
    }

    // 坡度线可以编辑
    if (const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement))
    {
        if (const IStructureSlopeLine* pSlope = quick_cast<IStructureSlopeLine>(pGenericElement->GetExternalObject()))
        {
            return true;
        }
    }

    return false;
}

void StructureRoofEditModeBehavior::ReportParents(gcmp::IDocument* pDoc, gcmp::IElementParentReporter& reporter) const
{
    IEditMode* pProfileEditMode = IEditMode::Get(pDoc, GetEditModeType());
    DBG_WARN_AND_RETURN_VOID_UNLESS(nullptr != pProfileEditMode, L"pProfileEditMode为空",L"GDMPLab",L"2024-03-30");
    reporter.ReportWeak(pProfileEditMode->GetEditingElementId());
    reporter.ReportWeak(GetEditingFamilyId());
    reporter.ReportWeak(GetEditingInstanceTypeId());
    reporter.ReportWeak(GetPresetElementId());
}

void StructureRoofEditModeBehavior::UpdateForWeakParentDeletion(gcmp::IDocument* pDoc, const std::set<gcmp::ElementId>& deletedElementIds)
{
    IEditMode* pProfileEditMode = IEditMode::Get(pDoc, GetEditModeType());
    DBG_WARN_AND_RETURN_VOID_UNLESS(nullptr != pProfileEditMode, L"pProfileEditMode为空",L"GDMPLab",L"2024-03-30");

    if (deletedElementIds.find(pProfileEditMode->GetEditingElementId()) != deletedElementIds.end())
    {
        DBG_WARN(L"不应该在EditMode有效时删除其中引用的Element",L"GDMPLab",L"2024-03-30");
    }

    if (deletedElementIds.find(GetPresetElementId()) != deletedElementIds.end())
    {
        SetPresetElementId__(ElementId::InvalidID);
    }

    if (deletedElementIds.find(GetEditingFamilyId()) != deletedElementIds.end())
    {
        SetEditingFamilyId__(ElementId::InvalidID);
    }

    if (deletedElementIds.find(GetEditingInstanceTypeId()) != deletedElementIds.end())
    {
        SetEditingInstanceTypeId__(ElementId::InvalidID);
    }
}

void StructureRoofEditModeBehavior::DeletePresetElementId(IDocument* pDoc)
{
    if (GetPresetElementId__().IsValid())
    {
        std::wstring cannotDeleteExplanation;
        bool bOk = pDoc->DeleteElement(GetPresetElementId__(), &cannotDeleteExplanation);
        DBG_WARN_AND_RETURN_VOID_UNLESS(bOk, L"删除预设参数对象失败",L"GDMPLab",L"2024-03-30");

        SetPresetElementId__(ElementId::InvalidID);
    }
}